<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require 'defaultController.php';

class loginController extends defaultController {

    private $error = array();

    function registerAction() {      
        $data['p']=$this->for_get('p');
        
        $this->_view('register.tpl.php',$data);
    }
    
    function indexAction(){
        $data['pai1']= $this->_model('task')->pai1();
        $data['pai2']= $this->_model('login')->pai2();
        $this->_view('login.tpl.php',$data);
    }
    
    function loginAction(){
        $name=$this->for_post('username');
        $password=$this->for_post('password');
        $result=$this->_model('login')->checkpassoword($name,$password);                
        if($result){  
            $_SESSION['userid']=$result['member_id'];
            $_SESSION['username']=$result['relname'];
            echo 1;
        }else{
            echo 0;
        }
    }
    
    function loginoutAction(){
        session_destroy();
        header("Location: index.php?c=index&a=index");
    }

    
    function checkregisterAction(){
            $this->validate();
            if($this->validate()){
                $data=array(
                    "name"=>$this->for_post('username'),
                    "password"=>$this->for_post('password'),
                    "email"=>$this->for_post('email'),
                    "relname"=>$this->for_post('relname'),
                    "phone"=>$this->for_post('phone'),
                    'QQ'=>$this->for_post('QQ'),
                    'friend'=>$this->for_post('friend')
                    );
                $result=$this->_model('login')->addmember($data);
                //$this->redirectPage("index.php?c=login&a=index");
                echo 1;
            }else{
                $index=0;
                foreach ($this->error as $value){
                    if($index==0){
                       $data= $value;
                       $index=2;
                    }
                }
                echo json_encode($data);
            }          
    }

    protected function validate() {    
        if(strlen($this->for_post('username'))<1||strlen($this->for_post('username'))>50){
            $this->error['username'] = '用户名不能为空，或者太长！';
        }
        if ((strlen($this->for_post('password')) < 6) || (strlen($this->for_post('password')) > 20)) {
            $this->error['password'] = '密码长度为6-20位！';
        }

        if ($this->for_post('confirm_password') != $this->for_post('password')) {
            $this->error['confirm'] = "两次密码不一致";
        }
        $pattern_test = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
       
        if ( !preg_match($pattern_test,$this->for_post('email'))){
                      $this->error['email'] = '邮箱格式不正确！';          
        }
        if(strlen($this->for_post('relname'))<1||strlen($this->for_post('relname'))>50){
            $this->error['relname'] = '真实姓名不能为空，或者太长！';
        }
        if (!preg_match("/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/",$this->for_post('phone'))) {
            $this->error['phone'] = '手机号码格式不正确！';
        }       
        if(!preg_match("/^\d{5,10}$/",$this->for_post('QQ'))){
            $this->error['QQ'] = 'QQ号格式不正确！';
        }
        if($result=$this->_model('login')->getOne($this->for_post('username'))){
            $this->error['have'] = '账号名字已存在！';
        }
        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }
}
